package dgut.haitaobao.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import dgut.haitaobao.service.ProductCommentsService;
import dgut.haitaobao.service.ProductParamsService;
import dgut.haitaobao.service.ProductService;
import dgut.haitaobao.util.response.HttpResponse;
import dgut.haitaobao.vo.param.PorductByCategoryIdParamsVO;
import dgut.haitaobao.vo.param.PorductByProductNameParamsVO;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
@CrossOrigin
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductParamsService productParamsService;

    @Autowired
    private ProductCommentsService productCommentsService;

    //获得详细商品信息(包括商品图片，商品sku)
    @PostMapping("/getDetailProducts")
    public HttpResponse getDetailProducts(@RequestBody String productId) throws JsonProcessingException {
        System.out.println(productId);
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(productId);
        String productText = jsonNode.get("productId").asText();
        return productService.getDetailProductByProductId(productText);
    }
    
    
    //根据商品id获得商品的参数信息
    @PostMapping("/getProductParams")
    public HttpResponse getProductParams(@RequestBody String productId) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(productId);
        String productText = jsonNode.get("productId").asText();
        return productParamsService.getProductParamsByProductId(productText);
    }

    //根据商品id来获得商品评论
    @PostMapping("/getProductComments")
    public HttpResponse  getProudctComments(@RequestBody String productId) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(productId);
        String productText = jsonNode.get("productId").asText();
        return productCommentsService.getProductCommentsByProductId(productText);
    }

    //根据三级分类id来查询商品中的信息
    @PostMapping("/getProductByCategoryId")
    public HttpResponse getProductByCategoryId(@RequestBody PorductByCategoryIdParamsVO paramsVO){
        System.out.println(paramsVO);
        return productService.getProductByCategoryId(paramsVO.getCategoryId(),paramsVO.getPageNum(),paramsVO.getLineNum());
    }

    //根据商品名称去模糊搜索商品信息
    @PostMapping("/getProductByProductName")
    public HttpResponse getProductByProductName(@RequestBody PorductByProductNameParamsVO paramsVO) throws JsonProcessingException {
        System.out.println(paramsVO);
        System.out.println(paramsVO.getPageNum());
        return productService.getProductByProductName(paramsVO.getProductName(),paramsVO.getPageNum(),paramsVO.getLineNum());
    }

}
